package com.gavin.lingxi.rtclib.util;

import android.os.Handler;
import android.os.Looper;

public final class ThreadUtils {

    private static Thread sMainThread = Looper.getMainLooper().getThread();
    private static Handler sMainHandler = new Handler(Looper.getMainLooper());

    private ThreadUtils() {
        // static usage.
    }

    public static void post(Runnable r) {
        sMainHandler.post(r);
    }

    public static void postDelayed(Runnable r, long delayMillis) {
        sMainHandler.postDelayed(r, delayMillis);
    }

    public static void removeCallbacks(Runnable r) {
        sMainHandler.removeCallbacks(r);
    }

    public static void runOnUiThread(Runnable runnable) {
        if (isMainThread()) {
            runnable.run();
        } else {
            post(runnable);
        }
    }

    public static boolean isMainThread() {
        return sMainThread == Thread.currentThread();
    }

    public static Handler getMainHandler() {
        return sMainHandler;
    }
}
